package com.example.thirdstage_project.controller;

/**
 * @author 86158
 * @version 1.0
 * Create by 2022/11/26 11:52
 */


import com.example.thirdstage_project.common.Code;
import com.example.thirdstage_project.common.Result;
import com.example.thirdstage_project.entity.Project;
import com.example.thirdstage_project.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目管理
 */


@RestController
@RequestMapping("/projects")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 查询
     * @return
     */
    @GetMapping
    public Result selectAll(){
        List<Project> list = projectService.list();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败";
        return new Result(code,list,msg);
    }

    /**
     * 删除
     */
    @DeleteMapping("/{projectId}")
    public Result delete(@PathVariable Integer id){
        boolean delete = projectService.removeById(id);
        return new Result(delete ? Code.DELETE_OK : Code.DELETE_ERR,delete);
    }

    /**
     * 更新
     */
    @PostMapping
    public Result update(Project project){
        boolean update = projectService.saveOrUpdate(project);
        return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERR,update);
    }




}
